/**  
* @Title: UrlEncode.java
* @Package org.zsen.crawler.util
* @Description TODO
* @author ZhangSen
* @date 2015年11月4日 上午11:35:32
* @version 
*/ 
package org.zsen.crawler.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Base64;

/**
* @ClassName: UrlEncode
* @Description: 
* @author ZhangSen
* @date 2015年11月4日 上午11:35:32
*
*/
public class XunboUrlEncoderUtils {
	private static final String thunderPrefix = "AA";
	private static final String thunderPosix = "ZZ";
	private static final String thunderTitle = "thunder://";
	public static String thunderEncode(String url)
	{
		return thunderTitle+Base64.getEncoder().encodeToString((thunderPrefix+url+thunderPosix).getBytes());
	}
	
	public static String thunderDecode(String thunderUrl)
	{
		return new String(Base64.getDecoder().decode(thunderUrl.replaceFirst(thunderTitle, ""))).replaceAll("^"+thunderPrefix+"|"+thunderPosix+"$", "");
	}
	
	public static String get_movie_name(String filename, String arsg) {
		  String[] tname = filename.split(arsg);
		  if (tname.length > 2) {
		    return get_movie_name(tname[(tname.length - 1)], "]");
		  } 
		  else
		  {
		    return tname[(tname.length - 1)];
		  }
		}
	
	public static String getNameByURL(String downurl){
	    String resultStr = downurl;
	    if(downurl.indexOf("ed2k://|file|")==0){
	        String[] tmpStr =  downurl.split("\\|");
	        if(tmpStr.length>3){
	            if(tmpStr[2].length()>0){
						try {
							resultStr = URLDecoder.decode(tmpStr[2],"utf-8");
						} catch (UnsupportedEncodingException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}	
	            }
	        }

	        return resultStr;
	    }else if(downurl.indexOf("ftp://")==0)
	    {
	    	
	        return resultStr.replaceFirst("(.+])", "");
	    }
	    else 
	    	return resultStr;
	}

}
